package cn.cihon.ubi.qorosauto.util;

public class MathUtil {

	private static int cntOfLimit = 0;
	private static int cntOfBig = 0;
	/**
	 * 根据传入的x^2和μ计算标准差，利用简化公式:
	 * σ = (sum(Xi^2)/N - μ^2)^(-2)
	 * @param a = sum(Xi^2)
	 * @param u = μ
	 * @param n = N,当n等于0时，结果返回0
	 * @return standardDeviation = σ
	 */
	public static Double calcStandardDeviation(Double a,Double u, Integer n) {
		Double standardDeviation = 0.0;
		if (n == 0) {
			return 0.0;
		}
		double data = a/n -Math.pow(u, 2);
		if (data < 0) {
			if (Math.abs(data) < 1e-10) {
				cntOfLimit ++;
				System.out.println("第" + cntOfLimit + "个可忽略数字");
				return 0.0;
			} else {
				cntOfBig ++;
				System.out.println("第" + cntOfBig + "个不可忽略数字");
			}
		}
		standardDeviation = Math.sqrt(a/n - Math.pow(u, 2));
		return standardDeviation;
	}


	/**
	 * 此方法适用n为Long类型
	 */
	public static Double calcStandardDeviation(Double a,Double u, Long n) {
		Double standardDeviation = 0.0;
		if (n == 0L) {
			return 0.0;
		}
		standardDeviation = Math.sqrt(a/n - Math.pow(u, 2));
		return standardDeviation;
	}
}
